package com.qd.common.panda.domain.enums;

import com.cdqidi.exception.ApiException;

import java.util.Arrays;

/**
 * 油号
 * 0	0号柴油
 * 92	92号汽油
 * 95	95号汽油
 * 98	98号汽油
 *
 * @author sjk
 */
public enum GasEnum {
    /**
     * 0号柴油
     */
    GAS_0(0, "0号柴油"),
    /**
     * 92号汽油
     */
    GAS_92(92, "92号汽油"),
    /**
     * 95号汽油
     */
    GAS_95(95, "95号汽油"),
    /**
     * 98号汽油
     */
    GAS_98(98, "98号汽油");

    final Integer code;
    final String msg;

    GasEnum(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Integer getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public static void checkOctaneNumber(Integer octaneNumber) {
        if (null == octaneNumber) {
            throw new ApiException("油号不能为空");
        }
        if (Arrays.stream(GasEnum.values()).filter(gasEnum -> gasEnum.getCode().equals(octaneNumber)).findFirst().isEmpty()) {
            throw new ApiException("油号不存在");
        }
    }

    /**
     * 提前判断，用于解决
     * Case中出现的Constant expression required
     *
     * @param octaneNumber 编码
     * @return 对应的枚举类
     */
    public static GasEnum getByValue(Integer octaneNumber) {
        for (GasEnum gasEnum : values()) {
            if (gasEnum.getCode().equals(octaneNumber)) {
                return gasEnum;
            }
        }
        return null;
    }
}
